<template>
  <com-card>
    <div slot="com_header" class="shop">
      <div>店铺管理 *00384730</div>
      <div>
        <el-button type="primary" size="small" @click="save">保存</el-button>
      </div>
    </div>
    <div slot="com_con">
      <el-row>
        <el-col :md="12">
          <el-form :model="formObj" label-width="100px">
            <el-form-item label="店铺名称">
              <el-input type="text" v-model="formObj.name"></el-input>
            </el-form-item>
            <el-form-item label="店铺公告">
              <el-input type="textarea" v-model="formObj.bulletin"></el-input>
            </el-form-item>
            <el-form-item label="店铺头像">
              <el-upload
                class="avatar-uploader"
                action="http://localhost:5000/shop/upload/"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
              >
                <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
            <el-form-item label="店铺图片">
              <el-upload
                action="http://localhost:5000/shop/upload/"
                list-type="picture-card"
                :file-list="fileList"
                :on-preview="handlePictureCardPreview"
                :on-remove="handleRemove"
                :on-success="shopImg_success"
              >
                <i class="el-icon-plus"></i>
              </el-upload>

              <el-dialog :visible.sync="dialogVisible">
                <img width="100%" :src="dialogImageUrl" alt="" />
              </el-dialog>
            </el-form-item>
            <el-form-item label="配送费">
              <el-input type="text" v-model="formObj.deliveryPrice"></el-input>
            </el-form-item>
            <el-form-item label="配送时间">
              <el-input type="text" v-model="formObj.deliveryTime"></el-input>
            </el-form-item>
            <el-form-item label="配送描述">
              <el-input type="text" v-model="formObj.description"></el-input>
            </el-form-item>
            <el-form-item label="评分">
              <el-input type="text" v-model="formObj.score"></el-input>
            </el-form-item>
            <el-form-item label="销量">
              <el-input type="text" v-model="formObj.sellCount"></el-input>
            </el-form-item>
            <el-form-item label="活动">
              <el-checkbox-group v-model="Formsupports">
                <el-checkbox label="满28减5" name="type"></el-checkbox>
                <el-checkbox
                  label="VC无限橙果汁全场8折"
                  name="type"
                ></el-checkbox>
                <el-checkbox label="精彩套餐" name="type"></el-checkbox>
                <el-checkbox label="特价八折" name="type"></el-checkbox>
                <el-checkbox label="单人精彩套餐" name="type"></el-checkbox>
              </el-checkbox-group>
            </el-form-item>
            <el-form-item label="营业时间">
              <el-time-picker
                is-range
                v-model="startTime"
                range-separator="至"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                placeholder="选择时间范围"
              >
              </el-time-picker>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
  </com-card>
</template>

<script>
import ComCard from "../../components/card";
import { api_query, api_update } from "../../api/shop.js";
export default {
  components: {
    ComCard,
  },
  data() {
    return {
       // 时间范围
      startTime: [],
      imageUrl: "",
      formObj: {
        id: "", //店铺id
        name: "", //店铺名称
        bulletin: "", //店铺公告
        avatar: "", //店铺头像
        deliveryPrice: "", //起送价格
        deliveryTime: "", //送达时间
        description: "", //店铺描述
        score: "", //店铺好评率
        sellCount: "", //店铺销量
        supports: "", //活动支持
        date: "", //营业时间
        pics: "", //店铺图片
      },

      //复选框 活动的数组
      Formsupports: [],

      // 多图上传数据
      //预览的图片地址
      dialogImageUrl: "",
      //预览的弹窗显示
      dialogVisible: false,
      //图片列表
      fileList: [],
    };
  },
  mounted() {
    this.query();
  },
  methods: {
    async query() {
      let rst = await api_query();
      let { data } = rst;
      let {
        id,
        name,
        bulletin,
        avatar,
        deliveryPrice,
        deliveryTime,
        description,
        score,
        sellCount,
        supports,
        date,
        pics
      } = data;
      this.formObj = {
        id,
        name,
        bulletin,
        avatar,
        deliveryPrice,
        deliveryTime,
        description,
        score,
        sellCount,
        supports,
        date,
        pics
      };
      this.imageUrl = `http://localhost:5000/upload/shop/${avatar}`;
      this.fileList=pics.map((item)=>{
        return {name:item,url:`http://localhost:5000/upload/shop/${item}`}
      })
      this.Formsupports=supports
      this.startTime=date
      console.log(data);
    },

    //上传单张图片头像
    handleAvatarSuccess(res, file) {
      console.log(res);
      this.imageUrl = `http://localhost:5000/upload/shop/${res.imgUrl}`;
      // this.formObj.avatar = imgUrl;
      // this.formObj.imgUrl=res.imgUrl
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg" || file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },

    // 多图上传
    //移除图片
    handleRemove(file, fileList) {
      // console.log(file, fileList);
      this.fileList = fileList.map((item) => {
        return { name: item.name, url: item.url };
      });
    },

    //预览图片
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    //多图上传成功
    shopImg_success(res, file) {
      let { code, msg, imgUrl } = res;
      let fileObj = {
        name: imgUrl,
        url: `http://localhost:5000/upload/shop/${imgUrl}`,
      };
      this.fileList.push(fileObj);
      // 将当前已经上传的图片名称添加到pics的数组中
      // this.formObj.pics.push(imgUrl)
      console.log(this.fileList);
    },

    //保存修改
  async save() {
    this.formObj.avatar=this.imageUrl.substr((this.imageUrl.lastIndexOf('/')+1))
    this.formObj.pics=this.fileList.map((item)=>{
      return item.name
    })
    this.formObj.pics=JSON.stringify(this.formObj.pics)
    this.formObj.date=JSON.stringify(this.startTime)
    this.formObj.supports=JSON.stringify(this.Formsupports)
      let rst =await api_update(this.formObj)
      console.log(this.formObj);
      console.log(rst);
      let {msg,code}=rst;
      if(code==0){
        this.$message.success(msg)
      }
      // this.formObj.avatar=this.imageUrl.substr((this.imageUrl.lastIndexOf("/")+1));
      //   this.formObj.pics=this.fileList.map((item)=>{
      //       return item.name;
      //   })
      //   this.formObj.pics=JSON.stringify(this.formObj.pics);
      //   this.formObj.supports=JSON.stringify(this.Formsupports);
      //   this.formObj.date=JSON.stringify(this.startTime);
      //  let rst= await api_update(this.formObj);
      //  let {code,msg}=rst;
      //  if(code==0){
      //      this.$message.success(msg);
      //  }
    },

  },
};
</script>

<style lang="less" scoped>
.shop {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
/deep/ .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>